package com.mydemo.game.resaultStage;

import com.mydemo.entity.Car;
import com.mydemo.entity.Driver;
import com.mydemo.entity.Save;

import java.util.ArrayList;
import java.util.List;

public class CarDaysCalculate {
    public static final Save carDaysCalculate(Save save) {
        List<Car> carList = save.getCarList(); // 创建副本
        if (carList!= null) {
            for (Car car : carList) {
                car.setDays(car.getDays() - 1);
                if (car.getDays() < 5) {
                    if (car.getDays() <= 0) {
                        System.out.println(car.getCarBrand() + car.getCarModel() + car.getCarModelDetail() + "租期已到，自动离开了");
                        if (save.getDriverList()!= null){
                            for (Driver driver :
                                    save.getDriverList()) {
                                if (driver.getCar() == car.getId())driver.setCar(0);
                            }
                        }
                    } else {
                        System.out.println("++++++++++++++++++++");
                        System.out.print("Id：" + car.getId());
                        System.out.println("车辆："+car.getCarBrand() + car.getCarModel() + car.getCarModelDetail());
                        System.out.println("↑ 的剩余日期不足五天，请续费");
                    }
                }
            }
            List<Car> carsToRemove = new ArrayList<>();
            for (Car car : carList) {
                if (car.getDays() <= 0) {
                    carsToRemove.add(car);
                }
            }
            save.getCarList().removeAll(carsToRemove);
        }

        return save;
    }
}
